/*
 * @Author: wuqingchun
 * @Date: 2024-07-23 11:19:12
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-26 09:05:44
 */
#ifndef __PAGE_CACHE_H__
#define __PAGE_CACHE_H__

#include <mutex>
#include <unordered_map>

#include "span.h"
#include "object_pool.h"


/**
 * @brief 用于管理从操作系统中分配出来的内存，从操作系统分配内存以页为单位进行申请
 * 
 */
class PageCache
{
public:
    static PageCache* GetInstance()
    {
        return &m_objInstance;
    }
    
    Span* NewSpan(size_t ulPageNum);

    Span* PopFront();
private:
    PageCache()
    {
        
    }

private:
    Span* __GetLargeSpan(size_t ulPageNum);

private:
    static PageCache m_objInstance;
private:
    SpanList m_arrSpanList[SizeAlignStrategy::PAGE_NUM_MAX];
    std::mutex m_arrListMtx[SizeAlignStrategy::PAGE_NUM_MAX];

    ObjectPool<Span> m_objSpanPool;

    std::unordered_map<unsigned long, Span*> m_mapSpanByPageID;
    std::mutex m_objMapMtx;
};

#endif //__PAGE_CACHE_H__
